<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/29
 * Time: 19:23
 */
require_once('../../vendor/autoload.php');

Logger::configure(array(
    'rootLogger' => array(
        'appenders' => array('default'),
    ),
    'appenders' => array(
        'default' => array(
            'class' => 'LoggerAppenderFile',
            'layout' => array(
                'class' => 'LoggerLayoutSimple'
            ),
            'params' => array(
                'file' => './my.log',
                'append' => true
            )
        )
    )
));
$logger = Logger::getLogger("test");

$redis = new \Redis();
$redis->connect('127.0.0.1', '6379');
$redis->select(0);


$redis->watch('goods_num');
$inventory = $redis->get('goods_num');
//由于本地服务过访问过快。所以休息50毫秒真实模拟高并发
if ($inventory <= 0) {
    echo "抢购失败！";
    $redis->close();
    exit();
}
$redis->multi();
$redis->decr("goods_num");
$user_id = rand(100000,999999);
$redis->lPush('user_id',$user_id);
$redis->exec();
$redis->close();